package addCompo.uiSheet
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Quint;
	
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class XMUISheet extends MovieClip
	{
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		public var bg : MovieClip;
		public var bg_top : MovieClip;
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		private var btnList : Vector.<SheetButton> = new Vector.<SheetButton>();
		private var titleList : Vector.<TextField> = new Vector.<TextField>();
		private var gapX : int = 5;
		private var gapY : int = 5;
		
		private var labelColor : uint = 0x000000;
		
		private var colorComplete : Boolean = false;
		
		private var $stage : Stage;
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		public function XMUISheet()
		{
			super();
			//
			this.x 				= 0;
			this.y				= stage.stageHeight;
			this.bg.width 		= stage.stageWidth;
			this.bg.height		= 10;
			this.bg_top.width 	= this.bg.width;
			
//			stage.addEventListener(MouseEvent.CLICK, onThisClick );
			
		}
		
		[Inspectable(name="BtnDataProvider", defaultValue="", type=Array )]
		public function set BtnDataProvider ( value : Array ):void
		{
			//초기화
			btnList.length = 0;
			
			//배열 개수에 따라서 bg 사이즈 조절
			resizeBGHeight( value.length );
			
			//배열 안에 있는 값으로 버튼 만들기
			var btn : SheetButton;
			var i : int = 0;
			var len : int = value.length;
			while( i < len )
			{
				btn = new SheetButton( value[i].toString() );
				btnList.push( btn );
				this.addChild( btnList[i] );
				btnList[i].width = this.bg.width - gapX*2;
				btnList[i].x = gapX;
				if( i == 0 ) btnList[i].y = this.height - btnList[i].height - gapY;
				else 		 btnList[i].y = btnList[i-1].y - btnList[i].height - gapY;
				btnList[i].addEventListener(MouseEvent.CLICK , onButtonClick );
				
				//텍스트 생성
				var textformat : TextFormat = new TextFormat( "돋움", 30, 0xffffff, true );
				
				var titleTF : TextField = new TextField();
				titleList.push( titleTF );
				titleTF.defaultTextFormat = textformat;
				titleTF.autoSize = TextFieldAutoSize.LEFT;
				titleTF.mouseEnabled = false;
				titleTF.selectable = false;
//				titleTF.text = "test"+i;
				titleTF.text = btn.btnLabel;
				this.addChild( titleTF );
				
				//텍스트 배치
				titleTF.x = btnList[i].x + (btnList[i].width - titleTF.width)/2;
				titleTF.y = btnList[i].y + (btnList[i].height - titleTF.height)/2;
				
				++i;
			}
			
		}
		
		[Inspectable(name="LabelColor", defaultValue="", type=Color )]
		public function set LabelColor ( value : Number ):void
		{
			var tf : TextFormat = new TextFormat();
			tf.color = value;
			
			var i : int = 0;
			var len : int = btnList.length;
			while( i < len )
			{
				if( btnList[i].btnType != '1' && btnList[i].btnType != '2' && btnList[i].btnType != '3' )	titleList[i].setTextFormat( tf );
				++i;
			}
		}
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		public function hideSheet():void
		{
			TweenMax.killTweensOf( this );
			TweenMax.to( this , 0.5 
				, {x:0,y:stage.stageHeight,ease:Quint.easeOut}
			);
		}
		
		public function showSheet():void
		{
			if( btnList.length == 0 ) return;
			if( this.y >= stage.stageHeight )
			{
				TweenMax.killTweensOf( this );
				TweenMax.to( this , 0.5 
					, {x:0,y:stage.stageHeight-this.height,ease:Quint.easeOut}
				);
			}
		}
		
		private function resizeBGHeight( count : int ):void
		{
			if( count <= 0 )
			{
				this.bg.height = (56+5);
			}else
			{
				this.bg.height = (56+5)*count;
			}
		}
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		protected function onThisClick(e:MouseEvent):void
		{
			showSheet();
		}
		
		protected function onButtonClick(e:MouseEvent):void
		{
			var currentTarget : SheetButton = e.currentTarget as SheetButton;
			//trace("===> UISheetButton , onButtonClick : " + currentTarget.btnType );
			//trace("===> UISheetButton , onButtonClick : " + currentTarget.width );
			hideSheet();
		}
	}
}